package main

type ListNode struct {
	Val  int
	Next *ListNode
}

func deleteDuplicates(head *ListNode) *ListNode {
	if head == nil || head.Next == nil {
		return head
	}
	slow := head
	fast := head.Next
	for fast.Next != nil {
		if slow.Val == fast.Val {
			fast = fast.Next
		} else {
			slow.Next = fast
			slow = fast
			fast = fast.Next
		}
	}
	// fast is the last node
	if fast.Val == slow.Val {
		slow.Next = nil
	} else {
		slow.Next = fast
	}
	return head
}

func main() {
	n0 := &ListNode{Val: 1}
	n1 := &ListNode{Val: 1}
	n2 := &ListNode{Val: 2}
	n0.Next = n1
	n1.Next = n2
	result := deleteDuplicates(n0)
	println(result)
}
